package com.hnkywl.sts.service.cwgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cwgl.GrjkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-6-4
 * Time: 上午9:45
 * To change this template use File | Settings | File Templates.
 */
@Service
public class GrjkService {
    private GrjkDao grjkDao;

    public String findAll(Date date,Long gsId,Long gsjgId,Long ygId){
        List<Map> grid = grjkDao.findAll(date,gsId,gsjgId,ygId);
        return "{\"total\":"+grid.size()+",\"rows\":"+new Gson().toJson(grid) +
                ",\"footer\":[{\"ygXm\":\"合计\",\"ye\":"+getSummerCount(grid)+"}]}";
    }

    public Double getSummerCount(List<Map> grid){
        Double f = 0.00;
        for(Map m : grid){
            BigDecimal df = new BigDecimal(Double.parseDouble(m.get("ye").toString()));
            f += df.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();;
        }
        return f;
    }

    public String findMx(Long ygId){
        List<Map> grid = grjkDao.findMx(ygId);
        return "{\"total\":"+grid.size()+",\"rows\":"+new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(grid) +"}";
    }

    @Autowired
    public void setGrjkDao(GrjkDao grjkDao) {
        this.grjkDao = grjkDao;
    }
}
